import request from "@/utils/request";
export const service = () => {
  return !!JSON.parse(localStorage.$loginInfo).tenantId ? "/event" : "/ttevent";
};
/**
 * 编辑常用语
 */
export function updateProcessCommonWords(data) {
  return request({
    url: service() + "/commonWords/updateProcessCommonWords",
    method: "post",
    data: data,
  });
}

/**
 * 查询常用语列表
 */
export function getProcessCommonWords(data) {
  return request({
    url: service() + "/commonWords/getProcessCommonWords",
    method: "post",
    data: data,
  });
}

/**
 * @param usefulWordList 修改后数组
 * @param oldUsefulWordList 修改前数组
 * @return 若没有修改则返回false，修改则返回params
 */
export function getUsefulWordsParams(usefulWordList, oldUsefulWordList) {
  let updateList = usefulWordList.filter((item, index) => {
    return item.content !== oldUsefulWordList[index].content;
  });
  if (updateList.length === 0) {
    return false;
  }
  return updateList.map((item) => {
    return {
      initWordsFlag: item.initWordsFlag,
      content: item.content,
    };
  });
}

/**
 * @param newUsefulWordList 更新后的数组
 * @param oldUsefulWordList 更新前的数组
 * @return 更新后的数组
 */
export function updateOldUsefulWords(newUsefulWordList, oldUsefulWordList) {
  oldUsefulWordList.map((i) => {
    newUsefulWordList.map((j) => {
      if (i.initWordsFlag === j.initWordsFlag) {
        i.content = j.content;
      }
    });
  });
  return oldUsefulWordList;
}

/**
 *
 * @param usefulWordList 修改后的数组
 * @param oldUsefulWordList 修改前的数组
 * @param _this this
 */
export function updateUsefulWords(usefulWordList, oldUsefulWordList, _this) {
  let params = getUsefulWordsParams(usefulWordList, oldUsefulWordList);
  if (!params) {
    return false;
  }
  _this.loading = true;
  updateProcessCommonWords(params).then((res) => {
    _this.loading = false;
    if (res.code == 200) {
      oldUsefulWordList = updateOldUsefulWords(params, oldUsefulWordList);
      _this.$message.success(res.msg);
    }
  });
}
